package com.gec.controller;

import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.ProcessConfig;
import com.gec.service.abst.WorkFlowService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/*
    名称: WorkFlowController
    父级地址: /WorkFlow
    主要工作: 负责布署流程定义文件。
                     查看流程布署信息。
                     删除暂时不做。
*/
@Controller
@RequestMapping("/Workflow")
public class WorkFlowController extends BaseController {

    //{1}自动装配一个 WorkflowService
    private final WorkFlowService workFlowService;

    public WorkFlowController(WorkFlowService workFlowService) {
        this.workFlowService = workFlowService;
    }

    /*
     * 映射地址: /deployProc
     * 功能: 执行流程布署
     */
    @ResponseBody
    @RequestMapping("/deployProcess")
    public String deployProc(
            @RequestParam("zipFile") MultipartFile file,
            @RequestParam("note") String note,
            @RequestParam("procCategory") String procCategory) {
        //{1}创建 Config 对象
        ProcessConfig config = new ProcessConfig();
        //{2}填入相应参数
        config.setNote(note);
        config.setProcCategory(procCategory);
        InputStream is;
        try {
            //{3}从 file 中获取输入流
            is = file.getInputStream();
            //{4}调用 service 执行流程布署
            workFlowService.addDeployment(config, is);
            return sendData("流程 ["+note+"] 部署成功");
        } catch (Exception e) {
            e.printStackTrace();
            //暂不处理(后面再加) ...
            return sendFailed(e);
        }
    }

    /*
     * 映射地址: /viewUploadProc
     * 功能: 显示文件上传页
     *      内部转发到 /WEB-INF/jsp/workflow/upload.jsp
     */
    @RequestMapping("/viewUploadProc")
    public String viewUploadProc() {
        return "workflow/upload";
    }


    @RequestMapping("/viewList")
    public String viewList() {
        return "workflow/list";
    }

    @ResponseBody
    @RequestMapping(value = "/jsonList", produces = JSON)
    public String jsonList(HttpServletRequest req) {
        PageParam param = PageParam.getPageParam(req);
        PageBean<ProcessConfig> pBean;
        try {
            pBean = workFlowService.getProcessList(param);
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }
    }

    @ResponseBody
    @RequestMapping("/delProcess")
    public String delProcess(@RequestParam("id") String id) {
        try {
            ProcessConfig config = workFlowService.getProcessConfigById(id);
            workFlowService.delProcessConfigById(id);
            return sendData("用户 [" + config.getNote() + "V" + config.getVersion() + "] 已被删除");
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

}
